package 代码coding题OR模拟题;
/**
 * 下面的 代码 通不过 
 * 
 */
import java.util.Arrays;

public class leetcode556大于但又最小 {
	public static void main(String []args) {
		System.out.println(nextGreaterElement(3360));
//		char []arr = {'1','2','3'};
//		
//		System.out.println(Arrays.toString(arr));
		
	}
	
	  public static int nextGreaterElement(int n) {
	        char [] arr = (n + "").toCharArray();
	        // Arrays.sort(arr);
	        int [] result = solution(n);
	        if(result == null)
	            return -1;
	        System.out.println(Arrays.toString(result));
	        char temp = arr[result[1]];
	        arr[result[1]] = arr[result[0]];
	        arr[result[0]] = temp;
	        
	        StringBuilder sb = new StringBuilder();
	        for(char ch : arr)
	        	sb.append(ch);
	        int ans = Integer.parseInt(sb.toString());
	        if(ans <= Integer.MAX_VALUE)
	            return ans;
	        return -1;


	    }
	    	public  static int []solution(int n) {
			char []arr = (n + "").toCharArray();
			int len = arr.length;
			for(int i = len - 1;i >= 0 ;i--) {
				for(int j = i - 1;j >= 0; j --) {
					if(arr[j] < arr[i] ) {
						return new int [] {i,j};
					}
				}
			}
			return null;
		}

}
